import {Collect} from '../../../models/collect'
import {check} from '../../../util/identityCheck'

import {Resume} from '../../../models/resume'

const resumeModel = new Resume()
let collectModel = new Collect()

Page({
  data: {
    page: 1,
    tabs: ['求职', '招聘', '招标'],
    tabIndex: 0,
    timeStamp: 0,
    showLoading: false,
    recruitData: [],
    resumeData: [],
    biddingData: [],
  },

  onShow () {
    if(!check())return
    this.init()
  },

  onLoad(options) {
    if(!check())return
    this.setData({
      tabIndex: options.tabIndex || 0
    })
    this.init()
  },

  chooseTab (event) {
    let type = event.type
    let timeStamp = event.timeStamp
    let index =  event.detail.current || event.target.dataset.index || 0
    if (type === 'change' && (timeStamp - this.data.timeStamp) < 50) {
      return
    }
    this.setData({
      tabIndex: index,
      timeStamp,
      page: 1
    })

    const tabIndex = this.data.tabIndex
    if (tabIndex == 0) {
      this.getResumeList()
    } else if (tabIndex == 1) {
      this.getRecruitList()
    } else if (tabIndex == 2) {
      this.getBiddingList()
    }
  },

  toNewPage (event) {
    let {id, path} = event.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/${path}/${path}?id=${id}`
    })
  },

  getResumeList (type) {
    resumeModel.getDeliveryResume({
      page: this.data.page,
    }, res => {
      this.setData({
        resumeData: type == 'more' ? [...this.data.resumeData, ...res] : res,
        showLoading: res.length >= 10
      })
    })
  },

  getRecruitList (type) {
    resumeModel.getDeliveryRecruit({
      page: this.data.page,
    }, res => {
      this.setData({
        recruitData: type == 'more' ? [...this.data.recruitData, ...res] : res,
        showLoading: res.length >= 10
      })
    })
  },

  getBiddingList (type) {
    resumeModel.getDeliveryBidding({
      page: this.data.page
    }, res => {
      this.setData({
        biddingData: type == 'more' ? [...this.data.biddingData, ...res] : res,
        showLoading: res.length >= 10
      })
    })
  },



  onPullDownRefresh () {
    const tabIndex = this.data.tabIndex
    this.setData({
      page: 1
    })
    if (tabIndex == 0) {
      this.getResumeList()
    } else if (tabIndex == 1) {
      this.getRecruitList()
    } else if (tabIndex == 2) {
      this.getBiddingList()
    }
  },

  onReachBottom () {
    const tabIndex = this.data.tabIndex
    this.setData({
      page: this.data.page + 1
    })
    if (tabIndex == 0) {
      this.getResumeList('more')
    } else if (tabIndex == 1) {
      this.getRecruitList('more')
    } else if (tabIndex == 2) {
      this.getBiddingList('more')
    }
  },

  init () {
    this.setData({
      page: 1
    })
    this.getResumeList()
  }
})
